package com.gotop.common.config;

import com.gotop.common.utils.support.XssFilter;
import com.gotop.common.utils.support.XssProperties;
import com.gotop.common.utils.support.XssUrlProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;

/**
 * @program: cem
 * @description: xss配置
 * @author: superfish
 * @create: 2020-12-28 18:00
 **/
@Configuration
@ConditionalOnProperty(value = {"cem.xss.enabled"}, havingValue = "true")
@EnableConfigurationProperties({XssProperties.class, XssUrlProperties.class})
public class XssConfiguration {
    private final XssProperties xssProperties;
    private final XssUrlProperties xssUrlProperties;

    @Bean
    public FilterRegistrationBean<XssFilter> xssFilterRegistration() {
        FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST, new DispatcherType[0]);
        registration.setFilter(new XssFilter(this.xssProperties, this.xssUrlProperties));
        registration.addUrlPatterns(new String[]{"/*"});
        registration.setName("xssFilter");
        registration.setOrder(2147483647);
        return registration;
    }

    public XssConfiguration(final XssProperties xssProperties, final XssUrlProperties xssUrlProperties) {
        this.xssProperties = xssProperties;
        this.xssUrlProperties = xssUrlProperties;
    }
}
